unit uArrumaNFCE;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Mask, JvExMask, JvToolEdit, JvMaskEdit, ExtCtrls, DB,
  MemDS, DBAccess, Uni;

type
  TFrmArrumaNFCe = class(TForm)
    pnl1: TPanel;
    grp1: TGroupBox;
    lbl1: TLabel;
    edt1: TJvMaskEdit;
    rg1: TRadioGroup;
    btn1: TButton;
    unqryTMP: TUniQuery;
    pnl2: TPanel;
    grp2: TGroupBox;
    btn2: TButton;
    procedure edt1KeyPress(Sender: TObject; var Key: Char);
    procedure visiblePanelNFCEDifPag;
    procedure reparaNFCEDifPagto ( IDMOV : string ; qry : TUniQuery );
    procedure rg1Click(Sender: TObject);
    procedure btn1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FrmArrumaNFCe: TFrmArrumaNFCe;

implementation

uses uDM;

{$R *.dfm}

procedure TFrmArrumaNFCe.btn1Click(Sender: TObject);
begin
  reparaNFCEDifPagto(edt1.Text,unqryTMP);
end;

procedure TFrmArrumaNFCe.edt1KeyPress(Sender: TObject; var Key: Char);
begin
  if not (Key in ['0'..'9',#8,#44]) then
    Key := #0;
end;

procedure TFrmArrumaNFCe.reparaNFCEDifPagto(IDMOV : String ; qry: TUniQuery);
var ListaIDPAGTO : TStringList;
  Erro : string;
begin
  Erro := '';
  ListaIDPAGTO := TStringList.Create;
  ListaIDPAGTO.StrictDelimiter := True;
  ListaIDPAGTO.Delimiter := ',';
  try
    try
      qry.Close;
      qry.SQL.Text := 'select idpagto from tmovpagto where idmov in (' + IDMOV + ')';
      qry.Open;
      qry.First;
      while not qry.Eof do
      begin
        ListaIDPAGTO.Add(qry.FieldByName('idpagto').AsString);
        qry.Next
      end;
      qry.Close;
      qry.SQL.Text := 'delete from tmovpagto where idmov in (' + IDMOV + ')';
      qry.ExecSQL;

      qry.Close;
      qry.SQL.Text := 'delete from tpagto where idpagto in (' + ListaIDPAGTO.DelimitedText + ')';
      qry.ExecSQL;

    except
      on e : Exception do
      begin
        Erro := 'ERRO:' + E.ClassName + #13 + E.Message;
        ShowMessage(erro);
      end;

    end;
  finally
    if Erro = '' then
    ShowMessage('PROCESSO CONCLUIDO SEM ERROS.')
    else
    ShowMessage('PROCESSO CONCLUIDO COM ERROS.');
    qry.Close;
    qry.SQL.Clear;
    ListaIDPAGTO.Free;
  end;

end;

procedure TFrmArrumaNFCe.rg1Click(Sender: TObject);
begin
  case rg1.ItemIndex of
    0 : visiblePanelNFCEDifPag;
  end;
end;

procedure TFrmArrumaNFCe.visiblePanelNFCEDifPag;
begin
  pnl1.Visible := True;
end;

end.
